﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using PGMHelper;
using PGMHelper.ProgressForm;
using Newtonsoft.Json;

namespace MultiProgressBar
{
    public partial class TestForm : Form
    {
        /// <summary>
        /// 单个分析步中的子步数目
        /// </summary>
        private int subStepNumber = 30;

        /// <summary>
        /// 线程数目
        /// </summary>
        private int threadNumber = 2;

        /// <summary>
        /// 总线程数
        /// </summary>
        private int threadTotalNumber = 5;

        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Shown(object sender, EventArgs e)
        {
            this.ToJson();
            ////随便传几个参数给各线程（可有可无）
            //var tuple = Tuple.Create<string, int>("今天天气", 444);
            ////简单进度条
            //Process.Run(this.SimpleProcessAction, ProgressType.Simple, tuple, "准备开启多线程");
            //Process.Run(this.SimpleProcessAction, ProgressType.OnePregress, tuple, "没想到还要多等一会");
            ////多线程开启
            //this.MultiProcessAction();
        }

        /// <summary>
        /// 简单的进度信息
        /// </summary>
        /// <param name="obj"></param>
        private void SimpleProcessAction(object obj)
        {
            //获得进度对象
            var process = (Process)obj;
            //获得其他参数
            var tuple = (Tuple<string, int>)process.Param;
            //设定最大值
            process.SetMainProcessBoundary(subStepNumber);
            //进度信息
            process.AddMessage(tuple.Item1 + tuple.Item2.ToString());
            //随着时间增加
            for (int i = 1; i <= subStepNumber; i++)
            {
                /**Do SomeThing*/
                Thread.Sleep(250);
                //进度计算
                process.AddMainProcess(1);
                process.AddMessage(string.Format("当前进度：{0}/{1}", i, subStepNumber));
            }
            process.CloseFrom(true);
        }

        /// <summary>
        /// 多线程
        /// </summary>
        private bool MultiProcessAction()
        {
            //各线程名称
            var descpList = new List<string>();
            //随便传几个参数给各线程（可有可无）
            var paramList = new List<object>();
            //遍历添加进程名称
            for (int i = 1; i <= this.threadTotalNumber; i++)
            {
                descpList.Add(string.Format("进程{0}", i));
                paramList.Add(Tuple.Create<string, int>("这是线程", i));
            }
            return MultiProcess.Run(this.SingleProcessAction, descpList, paramList, this.threadNumber);
        }

        /// <summary>
        /// 每个单线程进度
        /// </summary>
        /// <param name="obj"></param>
        private void SingleProcessAction(object obj)
        {
            //获得进度对象
            var process = (SubMultiProcess)obj;
            //获得其他参数
            var tuple = (Tuple<string, int>)process.Param;
            //进度信息 函数指针
            process.SetTaskDetail(process.Index, tuple.Item1 + tuple.Item2.ToString() + "开启");
            //随着时间增加
            for(int  i = 1; i <= subStepNumber; i++)
            {
                /**Do SomeThing*/
                Thread.Sleep(500);
                //进度计算
                var processValue = i / Convert.ToSingle(subStepNumber);
                //进度修改
                process.SetTaskProgress(process.Index, processValue);
                process.SetTaskSubProgress(process.Index, processValue);
            }
            //进度完成（美观）
            process.SetTaskDetail(process.Index, "完成");
            process.SetTaskProgress(process.Index, 1);
            process.SetTaskSubProgress(process.Index, 1);
            process.CloseForm(true);
        }

        /// <summary>
        /// 生成json文件
        /// </summary>
        private void ToJson()
        {
            var json = System.IO.File.ReadAllText("test.json");
            var paramSet = JsonConvert.DeserializeObject<PgmParamSet>(json);
            paramSet.ShowDialog();
        }
    }
}
